﻿namespace BEGDC.CC.ExcelTool
{
    #region Using Directives

    using System;
    using System.Configuration;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using ASBCC.Database.Entity;
    using ASBCC.Database.Service;
    using Excel;
    using Utility.ExceptionManagement;

    #endregion

    public class ExportCustomers : ICCExcelExport
    {
        private readonly ExcelExportCommom excelExport = new ExcelExportCommom();

        private readonly string folder = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ExportPath"]);
        private readonly string templateName = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["EZVisitCustomersImportTemplate"]);

        #region ICCExcelExport Members

        public void Export(Page page, int id)
        {
            string fileName = this.Export2Excel(id);

            this.excelExport.Export2Client(page, fileName, "EZVisitCustomers.xls");
        }

        #endregion

        private string Export2Excel(int appId)
        {
            try
            {
                this.excelExport.Open(this.templateName);

                var range = this.GetRange("Customers");

                var customers = EZVisitCustomerAttendeeManager.SelectByCondition("IsEnabled=1 and AppID=" + appId);

                int i = 1;

                foreach (EZVisitCustomerAttendeeTableEntity customer in customers)
                {
                    this.SetRange(range.Cells[i, 1], customer.NameCN);
                    this.SetRange(range.Cells[i, 2], customer.NameEN);
                    this.SetRange(range.Cells[i, 3], customer.CompanyCN);
                    this.SetRange(range.Cells[i, 4], customer.CompanyEN);
                    this.SetRange(range.Cells[i, 5], customer.TitleCN);
                    this.SetRange(range.Cells[i, 6], customer.TitleEN);
                    this.SetRange(range.Cells[i, 7], customer.Gender.Value ? "Male (男)" : "Female (女)");
                    this.SetRange(range.Cells[i, 8], customer.ResponsibilityText);
                    this.SetRange(range.Cells[i, 9], customer.Remark);

                    i++;
                }

                string fileName = Path.Combine(this.folder, string.Format("EZVC{0}-{1}-Customers.xls", appId, DateTime.Now.Ticks));

                this.excelExport.ExportAs(fileName);

                return fileName;
            }
            catch (Exception ex)
            {
                var exp = new GDCException("export_0001", ex.Message, ex.InnerException);
                throw exp;
            }
            finally
            {
                this.excelExport.Close();
            }
        }

        private void SetRange<T>(object cell, T value)
        {
            this.excelExport.MyApp.get_Range(cell, cell).Value2 = value;
        }

        private Range GetRange(string range)
        {
            return this.excelExport.MyApp.get_Range(range, Type.Missing);
        }
    }
}